package zisu.netty.practice.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/7/15 11:23
 */
public class NettyByteBuf02 {
    public static void main(String[] args) {
        //创建ByteBuf
        ByteBuf buf = Unpooled.copiedBuffer("hello,world!", Charset.forName("utf-8"));

        //使用相关的方法
        if(buf.hasArray()){
            byte[] array = buf.array();
            //将array 转成字符串
            System.out.println(new String(array,Charset.forName("utf-8")));

            System.out.println("byteBuf="+buf);
            System.out.println(buf.arrayOffset());  //0
            System.out.println(buf.readerIndex());  //0
            System.out.println(buf.writerIndex());  //12
            System.out.println(buf.capacity());     //36

            buf.readByte();

            int len =buf.readableBytes();
            System.out.println("len="+len);

        }
    }
}
